/*
 * Copyright (c) 2012 swing-on-fire Team
 * 
 * This file is part of Swing-On-Fire (http://code.google.com/p/swing-on-fire), licensed under the terms of the MIT
 * License (MIT).
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
 * documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
 * Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
 * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
 * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */
package com.google.code.swingonfire.showcase;

import javax.swing.JComponent;

import com.google.code.swingonfire.showcase.component.button.JMultiButtonShowcase;
import com.google.code.swingonfire.showcase.component.calendar.JCalendarShowcase;
import com.google.code.swingonfire.showcase.component.calendarclock.JCalendarClockShowcase;
import com.google.code.swingonfire.showcase.component.clock.JClockShowcase;
import com.google.code.swingonfire.showcase.component.foldable.JFoldablePanelShowcase;
import com.google.code.swingonfire.showcase.component.page.JPageComponentShowcase;
import com.google.code.swingonfire.showcase.component.page.JPagePaneShowcase;
import com.google.code.swingonfire.showcase.component.page.old.OldJPagePaneShowcase;
import com.google.code.swingonfire.showcase.graphic.sketch.SketchShowcase;
import com.google.code.swingonfire.showcase.helpers.gradients.GradientsShowcase;
import com.google.code.swingonfire.showcase.interpolation.InterpolationShowcase;
import com.google.code.swingonfire.showcase.utility.WindowUtilsShowcase;

public enum ShowcaseSamples
{
    TOPIC_COMPONENTS("Components", null),
    JMULTIBUTTON("JMultiButton", JMultiButtonShowcase.class),
    JCALENDAR("JCalendar", JCalendarShowcase.class),
    JCLOCK("JClock", JClockShowcase.class),
    JCALENDARCLOCK("JCalendarClock", JCalendarClockShowcase.class),
    JFOLDABLEPANEL("JFoldablePanel", JFoldablePanelShowcase.class),
    OLDJPAGEPANE("Old JPagePane", OldJPagePaneShowcase.class),
    JPAGECOMPONENT("JPageComponent", JPageComponentShowcase.class),
    JPAGEPANE("JPagePane", JPagePaneShowcase.class),
    TOPIC_GRAPHICS("Graphics", null),
    SKETCH("Plotter and Sketches", SketchShowcase.class),
    TOPIC_UTILITIES("Utilities", null),
    WINDOW_UTILS("WindowUtils", WindowUtilsShowcase.class),
    TOPIC_HELPERS("Helpers", null),
    INTERPOLATION("Interpolation", InterpolationShowcase.class),
    GRADIENTS("Gradients", GradientsShowcase.class);

    private final String name;
    private final boolean topic;
    private final Class<? extends JComponent> type;

    private JComponent instance;

    private ShowcaseSamples(String name, Class<? extends JComponent> type)
    {
        this.name = name;

        topic = type == null;

        this.type = type;
    }

    public String getName()
    {
        return name;
    }

    public boolean isTopic()
    {
        return topic;
    }

    public Class<? extends JComponent> getType()
    {
        return type;
    }

    public JComponent getInstance()
    {
        if (instance == null)
        {
            instance = newInstance();

            if (instance != null)
            {
                instance.setName(name);
            }
        }

        return instance;
    }

    private JComponent newInstance()
    {
        try
        {
            Class<? extends JComponent> type = getType();

            return (type != null) ? type.newInstance() : null;
        }
        catch (InstantiationException e)
        {
            throw new IllegalStateException("Failed to instantiate " + type);
        }
        catch (IllegalAccessException e)
        {
            throw new IllegalStateException("Failed to access " + type);
        }
    }

    @Override
    public String toString()
    {
        return name;
    }

}
